<?php

namespace App\Http\Forms;

use App\Library\OSS;
use App\Modules\Shop\Shop;
use Illuminate\Support\Facades\DB;
use Kris\LaravelFormBuilder\Form;

class ShopForm extends Form
{
    public function buildForm()
    {
        $parent = DB::select('SELECT * FROM categories WHERE PID =0 AND DELETED_AT IS NULL  ORDER BY SORT DESC');
        $parent = array_column($parent, "name", "id");
        $parent = [0 => "无"] + $parent;
        $key = "video" . "/" . uniqid() . random_int(10, 99);

        // 获取集团数据
        // $groupData = DB::table('groups')->pluck('name', 'id')->all();
        // array_unshift($groupData, '无');

        $this
            ->add('group_id', 'select', [
                'attr'  => ['class' => "form-control select2", 'id' => "groups-select"],
                'label' => "选择集团",
                'rules' => 'integer',
                'empty_value' => [0 => '无'],
                'choices' => $this->getData('groups')??[],
                // 'choices' => $groupData,
            ])
            ->add('name', 'text', [
                'label' => "门店名",
                'rules' => 'required|string',
            ])
            ->add('type_id', 'select', [
                'label' => '类别',
                'rules' => 'required|string',
                'choices' => $parent,
            ])
            ->add('phone', 'text', [
                'label' => "联系电话",
                'rules' => 'required|string',
            ])
            ->add('cover', 'image', [
                'label' => "封面图",
                'rules' => 'string',//required|string
                'type' => 'cover',
                'help_block' => [
                    'text' => "建议尺寸140*140",
                ]
            ])
            ->add('description', 'textarea', [
                'attr' => ['id' => 'editor'],
                'label' => "描述",
                'rules' => 'required|string',
            ])
            ->add('tag', 'text', [
                'label' => '备注',
                'rules' => 'nullable|string',
            ])
//            ->add('is_photography', 'select', [
//                'label' => '是否是摄影类门店',
//                'rules' => 'required|string',
//                'choices' => ["非摄影门店", "摄影门店"],
//            ])
            ->add('category_ids', 'select', [
                'attr' => ['class' => "form-control select2", 'multiple' => "multiple", 'id' => 'category'],
                'label' => "商品种类",
                'rules' => 'nullable|array',
                'choices' => $this->getData('category') ?? [],
            ])
            ->add('starting_price', 'text', [
                'label' => '起步价',
                'rules' => 'nullable|numeric',
                'default_value' => 0
            ])
            ->add('features', 'text', [
                'label' => '标签',
                'rules' => 'nullable',
            ])
            ->add('video', 'video', [
                'label' => "上传视频",
                'rules' => 'nullable',
                'async' => true,
                'config' => OSS::instance('video')->getSign($key),
            ])
            ->add('longitude', 'text', [
                'label' => '经度',
                'rules' => 'nullable|string',
            ])
            ->add('latitude', 'text', [
                'label' => '纬度',
                'rules' => 'nullable|string',
            ])
            ->add('submit', 'submit', [
                'attr' => ['class' => "form-control btn btn-success"],
                'label' => '保存',
            ]);
    }
}
